<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/assets/bootstrap/layui/layui/css/layui.css"  media="all">
    <style>
        body{
            background: #fff;
            padding: 10px 15px;
        }
        #lay-table {
            background-color: #fff;
            color: #666;
            border-collapse: collapse;
            border-spacing: 0;
        }

        #lay-table td {
            border-width: 1px;
            border-style: solid;
            border-color: #e6e6e6;
        }
        .btn-primary {
            background-color: #33cabb;
            border-color: #33cabb;
            color: #fff!important;
        }
        .btn-success {
            background-color: #15c377;
            border-color: #15c377;
            color: #fff!important;
        }
        .btn-warning {
            background-color: #faa64b;
            border-color: #faa64b;
            color: #fff!important;
        }
        .btn-danger {
            background-color: #f96868;
            border-color: #f96868;
            color: #fff!important;
        }
    </style>
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm btn-primary" onclick="createDir()">新建文件夹</button>
        <button class="layui-btn layui-btn-sm btn-danger" lay-event="getCheckData">删除</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="copyLink layui-btn btn-primary layui-btn-xs" lay-event="copy" ><i class="layui-icon" onmouseover="layer.tips('点击上传', this, {tips: [3, '#000']});">&#xe681;</i></a>
    <a class="copyLink layui-btn btn-primary layui-btn-xs" lay-event="gallery" ><i class="layui-icon" onmouseover="layer.tips('点击查看', this, {tips: [3, '#000']});">&#xe705;</i></a>
    <a class="layui-btn btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon">&#xe640;</i> </a>
</script>

<script src="/assets/bootstrap/layui/layui/layui.js" charset="utf-8"></script>
<script src="/assets/bootstrap/js/clipboard/clipboard.min.js"></script>
<script>
    layui.use(['jquery','table','element','layer'], function(){
        var table = layui.table,form = layui.form,layer = layui.layer,
            $ = layui.$,element = layui.element;

        table.render({
            elem: '#test'
            ,url:'/api/dir/list'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'name', title:'文件夹名称'}
                // ,{field:'Status', title:'状态',width: 100,templet:function (res) {
                //         if (res.Status == "1"){return '<button class="layui-btn layui-btn-xs btn-success">未使用</button>'}
                //         if (res.Status == "2"){return '<button class="layui-btn layui-btn-xs btn-danger">已使用</button>'}
                //     }}
                ,{field:'created_at', title:'生成日期',  edit: 'text', sort: true}
                ,{fixed:'right', title:'操作', toolbar: '#barDemo', width:140}
            ]]
            ,page: true
            ,done:function (res,currentCount) {
                element.render();
            }
        });
        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    layer.confirm('真的删除吗？', function(index){
                        var index = layer.load(1, {
                            shade: [0.3,'#000'] //0.1透明度的白色背景
                        });
                        var data = checkStatus.data;
                        var ids = new Array();
                        for(let i in data){
                            ids.push(data[i].Id)
                        }
                        $.ajax({
                            //几个参数需要注意一下
                            type: "POST",//方法类型
                            dataType: "json",//预期服务器返回的数据类型
                            url: "/system/code_dels" ,//url
                            data: {"ids":ids},
                            traditional:true,
                            success: function (result) {
                                if (result.code === 200) {
                                    layer.close(index);
                                    layer.msg('删除成功')
                                    setTimeout(function () {
                                        location.reload();
                                    },1000)
                                }else{
                                    layer.close(index);
                                    layer.msg(result.msg);
                                }
                            },
                        });
                    });
                    break;
            };
        });
        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确认删除该文件夹？', function(index){
                    $.post("/api/dir/delete",{"id":data.id},function (res) {
                        if (res.code === 200){
                            layer.msg("删除成功");
                            setTimeout(function () {
                                location.reload();
                            },1000)
                        }else{
                            layer.msg(res.msg);
                        }
                    })
                    layer.close(index);
                });
            } else if(obj.event === 'copy'){
                layer.open({
                    type: 2,
                    title: '上传文件',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['600px', '500px'],
                    content: '/file/upload?dir=' + data.name //iframe的url
                });
            }else if(obj.event === 'gallery'){
                layer.open({
                    type: 2,
                    title: '查看文件',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['650px', '500px'],
                    content: '/file/gallery?dir=' + data.name //iframe的url
                });
            }
        });
    });

    function createDir(){
        layer.open({
            type: 2,
            title: '新建文件夹',
            shadeClose: true,
            shade: 0.8,
            area: ['380px', '300px'],
            content: '/dir/form' //iframe的url
        });
    }
</script>

</body>
</html>